const router = require("koa-router")();
const base = require("../../models/Fjxx");
const mongoose=require("mongoose")
router.prefix("/sys/account");

//根据班级号获取所有学生绑定信息
router.get("/getStudentlistByClassNo", async (ctx, next) => {
  let classno = ctx.query.classno;
  let userinfo = ctx.request.token;
  // let classno=userinfo.classno;
  try {
    // let res = await base.myFindByQuery(
    //     {
    //         "baseinfo.classno":classno,
    //        "accountType": "学生",
    //     },
    //     {
    //         "username":1,
    //         "gender":1,
    //         "wxopenid":1,
    //     }
    // )
    let res = await base.myAggregate([
      {
        $match: {
          accountType: "学生",
          classno: classno,
        },
      },
      // {
      //     $lookup: {
      //         from: "baseExtendInfo",
      //         localField: "_id",
      //         foreignField: "baseid",
      //         as: "baseExtendInfo"
      //     }
      // },
      // {
      //     $unwind: {path:"$baseExtendInfo",preserveNullAndEmptyArrays:true}
      // },
      // {
      //     $match: {
      //         "baseExtendInfo.班级": classno
      //     }
      // },
      {
        $project: {
          _id: 1,
          grade: 1,
          username: 1,
          gender: 1,
          classno: 1,
          wxopenid: {
            $cond: {
              if: { $ifNull: ["$wxopenid", false] },
              then: "已绑定",
              else: "未绑定",
            },
          },
        },
      },
    ]);
    ctx.body = { error: false, result: res };
  } catch (err) {
    ctx.body = { error: true, message: "读取班主任学生时出错" };
  }
});

//根据年级获取所有学生绑定信息
router.get("/getStudentlistByGrade", async (ctx, next) => {
  let grade = ctx.query.grade;
  let userinfo = ctx.request.token;
  try {
    let res = await base.myAggregate([
      {
        $match: {
          accountType: "学生",
          grade: grade,
        },
      },
      {
        $project: {
          _id: 1,
          grade: 1,
          classno: 1,
          username: 1,
          gender: 1,
          wxopenid: {
            $cond: {
              if: { $ifNull: ["$wxopenid", false] },
              then: "已绑定",
              else: "未绑定",
            },
          },
        },
      },
    ]);
    ctx.body = { error: false, result: res };
  } catch (err) {
    ctx.body = { error: true, message: "读取班主任学生时出错" };
  }
});
//获取年级列表及班级列表
router.get("/getGradeAndClass", async (ctx, next) => {
  try {
    let res1 = await base.myAggregate([
      { $match: { accountType: "学生" } },
      {
        $group: {
          _id: "$grade",
        },
      },
      { $sort: { _id: 1 } },
    ]);
    let res2 = await base.myAggregate([
      { $match: { accountType: "学生" } },
      {
        $group: {
          _id: "$classno",
        },
      },
      { $sort: { _id: 1 } },
    ]);
    ctx.body = { error: false, result: { grade: res1, class: res2 } };
  } catch (err) {
    ctx.body = { error: true, message: "读取年级班级列表时出错" };
  }
});

//根据年级获取班级列表
router.get("/getClassByGrade", async (ctx, next) => {
  let grade = ctx.query.grade;
  try {
    let res = await base.myAggregate([
      { $match: { accountType: "学生", grade: grade } },
      {
        $group: {
          _id: "$classno",
        },
      },
      { $sort: { _id: 1 } },
    ]);
    ctx.body = { error: false, result: res };
  } catch (err) {
    ctx.body = { error: true, message: "读取年级班级列表时出错" };
  }
});

//获取登录用户的基础信息
router.get("/getbaseinfo", async (ctx, next) => {
  let wxuserinfo = ctx.request.token;
  try {
    ctx.body = { error: false, result: wxuserinfo };
  } catch (err) {
    ctx.body = { error: true, message: "获取base数据时出错！" };
  }
});

//获取登录用户的基础信息和扩展信息
router.post("/getFullInfo",async(ctx,next)=>{
  let wxuserinfo=ctx.request.token;
  let finduser = await base.myAggregate([
    {
      $match: {
        _id: mongoose.Types.ObjectId(wxuserinfo.userid),
      },
    },
    {
      $lookup: {
        from: "baseExtendInfo",
        localField: "_id",
        foreignField: "baseid",
        as: "baseExtendInfo",
      },
    },
    {
      $unwind: {
        path: "$baseExtendInfo",
        preserveNullAndEmptyArrays: true,
      },
    },
    {
      $project: {
        _id: 1,
        username: 1,
        gender: 1,
        wxopenid: 1,
        role: 1,
        accountType: 1,
        grade: 1,
        "baseExtendInfo.班级": 1,
        "baseExtendInfo.学籍号": 1,
        "baseExtendInfo.身份证件号":1,
        "baseExtendInfo.出生日期":1,
        "baseExtendInfo.民族":1,
        "baseExtendInfo.家庭地址":1,
        "baseExtendInfo.现住址":1,
        "baseExtendInfo.班级":1,
        "baseExtendInfo.born":1,
        "baseExtendInfo.家庭成员姓名":1,
        "baseExtendInfo.家庭成员关系":1,
        "baseExtendInfo.家庭成员联系电话":1,
      },
    },
  ]);
  ctx.body={error:false,result:finduser[0]}
})

module.exports = router;
